草庐IT

C++ typedef 结构与类

全部标签

struct - 如何从函数访问结构的实例字段?

假设我有一个Graph结构,如下所示:typeGraphstruct{nodes[]intadjListmap[int][]int}//somemethodsonthestruct//constructorfuncNew()*Graph{g:=new(Graph)g.adjList=make(map[int][]int)returng}现在,我创建了该结构的一个新实例,其中:aGraph:=New()。如何访问Graph结构(aGraph)的这个特定实例的字段?换句话说,我如何访问aGraph版本的nodes数组(例如,从另一个顶级函数中)?非常感谢任何帮助!

go - 在 Go 中查找结构的底层匿名字段类型

我有这两个结构:typeCustomTimestruct{time.Time}typeEventsstruct{TimestampCustomTime}当我反射(reflect)Events.Timestamp的字段时,我得到CustomTime;我怎样才能得到实际的底层类型time.Time? 最佳答案 这是一个goplayground示例,展示了如何访问匿名字段。https://play.golang.org/p/yQULMVaQK0基本上,一旦您获得了结构的值,您就应该能够从字段0中获取时间值

sorting - 对具有公共(public)字段的不同结构进行排序的最佳解决方案

我有这样的结构类型typeAstruct{NamestringCreatedAttime.Time...}typeBstruct{TitlestringCreatedAttime.Time...}typeCstruct{MessagestringCreatedAttime.Time...}还有一个通用slicevarresult[]interface{}包含A、B和C元素(将来还会有更多元素)我想按“CreatedAt”对slice进行排序。什么是最好的解决方案?我想避免检查类型或转换... 最佳答案 无论如何,您可以拥有包含这两种

go - 继续-无法解析嵌套结构

Closed.ThisquestiondoesnotmeetStackOverflowguidelines。它当前不接受答案。想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。去年关闭。Improvethisquestion我有以下结构typelogsstruct{EmailstringAccountstringBranchNamestring`json:"branch_name"`TokenstringActions[]action}typeactionstruct{timestringnamestringdatastring}和代码解析funclogsHandl

arrays - 自己结构数组的JSON编码

我尝试读取一个目录并从文件条目中生成一个JSON字符串。但是json.encoder.Encode()函数只返回空对象。为了测试,我在tmp目录中有两个文件:test1.jstest2.jsgo程序是这样的:packagemainimport("encoding/json""fmt""os""path/filepath""time")typeFilestruct{namestringtimeStampint64}funcmain(){files:=make([]File,0,20)filepath.Walk("/home/michael/tmp/",func(pathstring,fo

Go Lang-将结构附加到 slice

是否可以将struct附加到slice?任何人都可以张贴一个例子吗?此slice需要具有struct值。testSlice=make([]Row,10)我试过用这种方式追加,但没有用。testSlice.append(row) 最佳答案 testSlice=append(testSlice,Row{/*...*/}) 关于GoLang-将结构附加到slice,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.c

go - go中结构的内存分配

关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭4年前。Improvethisquestion我最近遇到了http://golang-sizeof.tips/这解释了如何为结构分配内存。我知道为了确保连续的内存分配,我们在为没有填充的变量分配内存时添加填充将不会获得连续的内存。所以我在我的64位计算机上测试了各种组合,发现网站上的结果和我的计算机上的结果不匹配。这是针对这种情况的:typeS2struct{astringbboolebooldint32fboolcstring}主要是,以下

json - 将 "{}"主体解码为结构时,Golang 不会产生错误

在restapi中,当body设置为“{}”时,jsonDecoder不会产生错误。这使得有必要检查目标结构是否仍为nil。我需要检查库是否应该像这样工作,或者这是否是它的问题。//ClientSidethisrequestreq,err:=http.NewRequest("POST","url",strings.NewReader("{}"))//Curlequivalent:curl-XPOST-d'{}'http://api:8080/r/primitives/multiply//ServersidetypeOperandsstruct{Values[]float64`json:

go - 将结构保存到 json 时运行时 : goroutine stack exceeds 1000000000-byte limit,

关闭。这个问题需要debuggingdetails.它目前不接受答案。编辑问题以包含desiredbehavior,aspecificproblemorerror,andtheshortestcodenecessarytoreproducetheproblem.这将有助于其他人回答问题。关闭3年前。Improvethisquestion我已经定义了一个gostruct的Trie数据结构。typeNodestruct{ValruneIsWordboolIsRootboolParent*NodeChildrenmap[rune]*Node}typeTriestruct{Root*Node}

go - 为什么在遍历结构片段时无法访问结构字段

我在包中有一个函数,该函数返回我在包中定义的特定结构的slice,然后将其导入到另一个主包中,我想遍历该函数返回的slice并访问structs字段,我收到“未定义”错误。“一个未定义(无法引用未导出的字段或方法a)”我想念什么?谢谢您的帮助。软件包代码:packagetest_packageimport"fmt"typeStructstruct{astringbstring}funcReturnStructSlice()([]Struct){s1:=Struct{"a","b"}s2:=Struct{"c","d"}structSliceToReturn:=[]Struct{s1,s